上次我們介紹到把大致上所有的鬧鐘基本資料編輯也已經存到單例裡面了,接下來就是可以將資料傳到 Realm
不過在上傳到 Realm 之前,我要先補充一下關於重複選擇天數的程式碼
在天數選擇完之後,這個頁面會顯示以選擇哪幾天,若沒有選擇天數的話,則上面會顯示 “Never” 反之勾選每一天的話,他會顯示 “Every Day”
我們要先建立一個字串陣列,裡面存放每一天的簡寫
let day_array = ["Sun " , "Mon " , "Tues " , "Wed " , "Thur " , "Fri " , "Sat "]
再來我們可以設定一個字串變數(final_check)存放 day_array 陣列裡的值,變成字串(最後可以用這個字串直接去變更主頁面的重複天數 Button)
之後我們可以寫一個 If 判斷,去判斷我有沒有選到全部的天數、什麼都沒選,或者是有哪幾天,而將內容放進 字串陣列(final_check)中
記得這個 Fuction 要寫在 viewdidapear 因為是每次進到這個頁面都要再做一次
func add_day() { //新增重複天數的函數
var final_check : String = ""
if day_value.shared.select.count == 7 {
day_insert.setTitle("Every Day", for: .normal)
}
else if day_value.shared.select.count == 0 {
day_insert.setTitle("Never", for: .normal)
}
else {
for i in 1...(day_value.shared.select.count) {
final_check.append(day_array[day_value.shared.select[i-1]])
}
day_insert.setTitle("\(final_check)", for: .normal)
}
}
因為每一次進行資料變更,每一項選項 Button 的 Text 都需要做更動,於是我們必須把程式碼打在 viewdidapear 中
override func viewDidAppear(_ animated: Bool) {
add_day() //新增重複天數的函數
label_insert.setTitle(label_text.shareinstance.label, for: .normal)
sound_insert.setTitle(Sound_select.sharedsound.sound_select, for: .normal)
sound_insert.setTitle(Sound_select.sharedsound.sound_select, for: .normal)
}
把以上功能寫出來之後,我們要把這些資料彙整到 Realm !
因為當我們在主頁面點選他時鐘的時候也會跳到這個頁面,但是並不是新增,而是修改點選時鐘的值,所以我設了一個 if 來判斷我是要修改資料還是新增資料!
(alarm_edit.edit_value_shared.edit_value 是我在主頁面建立的單例)
@IBAction func save(_ sender: Any) {
if alarm_edit.edit_value_shared.edit_value == false {
let realm = try! Realm()
let table : alarm_realm = alarm_realm()
table.hour = hour_select
table.minute = minute_select
for i in 0...day_value.shared.select.count-1 {
switch day_value.shared.select[i] {
case 0:
table.Sun = true
case 1:
table.Mon = true
case 2:
table.Tues = true
case 3:
table.Wed = true
case 4:
table.Thur = true
case 5:
table.Fri = true
case 6:
table.Sat = true
default:
break
}
}
table.label = label_text.shareinstance.label
table.sound = Sound_select.sharedsound.sound_select
table.snooze = snooze.isOn
table.alarm_switch = true
try! realm.write {
realm.add(table)
}
print("fileURl:\(realm.configuration.fileURL!)")
self.navigationController?.popViewController(animated: true)
} //新增資料
else {
let realm = try! Realm()
let table = realm.objects(alarm_realm.self)
let edit_alarm = table[alarm_didselect.edit_value_shared.select_index!]
try! realm.write {
edit_alarm.hour = hour_select
edit_alarm.minute = minute_select
edit_alarm.Sun = false
edit_alarm.Mon = false
edit_alarm.Tues = false
edit_alarm.Wed = false
edit_alarm.Thur = false
edit_alarm.Fri = false
edit_alarm.Sat = false
for i in 0...day_value.shared.select.count-1 {
switch day_value.shared.select[i] {
case 0:
edit_alarm.Sun = true
case 1:
edit_alarm.Mon = true
case 2:
edit_alarm.Tues = true
case 3:
edit_alarm.Wed = true
case 4:
edit_alarm.Thur = true
case 5:
edit_alarm.Fri = true
case 6:
edit_alarm.Sat = true
default:
break
}
}
edit_alarm.label = label_text.shareinstance.label
edit_alarm.sound = Sound_select.sharedsound.sound_select
edit_alarm.snooze = snooze.isOn
alarm_edit.edit_value_shared.edit_value = false
self.navigationController?.popViewController(animated: true)
}
}
} //修改資料
上面的判斷有你幾天天數是重複的方式,我是使用 Boolean 的型別來儲存的!
顧名思義就是將所有的編輯資料全部刪除之後再回到主頁面!
@IBAction func delete_alarm(_ sender: Any) {
if alarm_didselect.edit_value_shared.select_index != nil {
let realm = try! Realm()
let table = realm.objects(alarm_realm.self)
let edit_alarm = table[alarm_didselect.edit_value_shared.select_index!]
try! realm.write {
realm.delete(edit_alarm)
}
self.navigationController?.popViewController(animated: true)
}
else {
self.navigationController?.popViewController(animated: true)
}
}